﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using AfvandingService;
using System.Data.SqlClient;
using System.Data;
using System.Threading;

namespace AfvandingKlient
{
    public partial class Default : System.Web.UI.Page
    {
        private ICentral central;
        private ChannelFactory<ICentral> proxy;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsCallback)
            {
                proxy = new ChannelFactory<ICentral>(new NetTcpBinding(), "net.tcp://localhost:12345/");
                central = proxy.CreateChannel();
            }

            try
            {
                gridPumpestationer.DataSource = central.Pumpestationer();
                DataBind();
            }

            catch
            {
                Response.Write(
                    "<SCRIPT LANGUAGE=\"JavaScript\">alert(\"Kunne ikke oprette forbindelse til webservicen.\")</SCRIPT>");
            }
        }

        protected void btnOpretPumpestation_Click(object sender, EventArgs e)
        {
            Session["proxy"] = proxy;
            Session["OpdatérEllerOpret"] = "Opret";
            Response.Redirect("PumpeStation.aspx");
        }

        protected void btnOpdatérPumpestation_Click(object sender, EventArgs e)
        {
            try
            {
                Session["proxy"] = proxy;
                Session["OpdatérEllerOpret"] = "Opdatér";
                Session["PSnummer"] = gridPumpestationer.SelectedDataKey.Value;
                Response.Redirect("PumpeStation.aspx");
            }

            catch
            {
                Response.Write(
                    "<SCRIPT LANGUAGE=\"JavaScript\">alert(\"Vælg venligst en række før du opdaterer!\")</SCRIPT>");
            }
        }

        protected void btnSletPumpestation_Click(object sender, EventArgs e)
        {
            central.SletPumpestation(int.Parse(gridPumpestationer.SelectedDataKey.Value.ToString()));
        }

        protected void btnOpretPumpe_Click(object sender, EventArgs e)
        {
            central.AnmodOpretPumpe(int.Parse(gridPumpestationer.SelectedDataKey.Value.ToString()), 15, 2);
        }

        protected void btnOpdaterPumpe_Click(object sender, EventArgs e)
        {

        }

        protected void btnSletPumpe_Click(object sender, EventArgs e)
        {
            central.AnmodSletPumpe(int.Parse(gridPumpestationer.SelectedDataKey.Value.ToString()),
                int.Parse(gridPumper.SelectedDataKey.Value.ToString()));
        }

        protected void btnStartPumpe_Click(object sender, EventArgs e)
        {
            central.AnmodStart((int)Session["Pumpenr"], (int)Session["Pumpestationsnr"]);
        }

        protected void btnStopPumpe_Click(object sender, EventArgs e)
        {
            central.AnmodStop((int)Session["Pumpenr"], (int)Session["Pumpestationsnr"]);
        }

        protected void btnStatus_Click(object sender, EventArgs e)
        {
            double resultat =
                central.PumpestationVandstand(int.Parse(gridPumpestationer.SelectedDataKey.Value.ToString()));
            Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">alert(" + resultat + ")</SCRIPT>");


        }

        protected void btnStatuses_Click(object sender, EventArgs e)
        {
            List<double> resultatListe = central.PumpestationerVandstand();

            foreach (var item in resultatListe)
            {
                Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">alert(" + item + ")</SCRIPT>");
            }

        }

        protected void gridPumpestationer_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Pumpe> pumper = central.Pumper(int.Parse(gridPumpestationer.SelectedDataKey.Value.ToString()));
            Session["Source"] = pumper;
            gridPumper.DataSource = pumper;
            gridPumper.DataBind();
            Session["Pumpestationsnr"] = int.Parse(gridPumpestationer.SelectedDataKey.Value.ToString());
            lblSelectedPumpStation.Text = gridPumpestationer.SelectedDataKey.Value.ToString();
            try
            {
                chartVandstand.DataBindTable(central.VandstandsLog((int)Session["Pumpestationsnr"]));
            }
            catch (Exception exception) { Response.Write("Der findes ingen vandstandslog for denne pumpestation"); }
      
        }


        protected void gridPumper_SelectedIndexChanged1(object sender, EventArgs e)
        {

            gridPumper.DataSource = Session["Source"];
            gridPumper.DataBind();
            Session["Pumpenr"] = int.Parse(gridPumper.SelectedDataKey.Value.ToString());
            lblSelectedPump.Text = gridPumper.SelectedDataKey.Value.ToString();
        }
    }
}